package com.redpacketcover.rc.base;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import com.redpacketcover.rc.common.CustomProgressDialog;

import java.lang.reflect.Field;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import me.yokeyword.fragmentation.SupportActivity;

public abstract class BasicActivity<P extends BasicIPresenter> extends SupportActivity implements BasicView {

    private Unbinder mUnBinder;
    protected CustomProgressDialog mProgressDialog;

    protected P mPresenter;

    protected abstract int initLayout();

    protected abstract P createPresenter(); //MVP模式重写这个方法，如果不用MVP模式忽略此方法即可

    protected abstract void initViews();

    protected abstract void initData(); //初始化数据


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (initLayout() != 0) {
            setContentView(initLayout());
            mUnBinder = ButterKnife.bind(this);
        }
        mPresenter = createPresenter();
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
        initViews();
        initData();

    }

    @Override
    public void showLoadingDialog() {
        showLoadingDialog("", true);
    }

    @Override
    public void showLoadingDialog(String text) {
        showLoadingDialog(text, true);
    }

    /**
     * cancelEnable 点击屏幕外部 是否可以取消弹框，默认取消
     */
    protected void showLoadingDialog(String text, boolean cancelEnable) {
        if (isFinishing()) {
            return;
        }
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            return;
        }
        mProgressDialog = new CustomProgressDialog(BasicActivity.this);
        mProgressDialog.setCancelable(cancelEnable);
        mProgressDialog.setCanceledOnTouchOutside(cancelEnable);
        mProgressDialog.setMessage(text);
        mProgressDialog.show();
    }

    @Override
    public void dismissLoadingDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        if (mUnBinder != null) {
            mUnBinder.unbind();
        }

        if (null != mProgressDialog) {
            mProgressDialog.dismiss();
            mProgressDialog.cancel();
            mProgressDialog = null;
        }

    }

    public void setHideStatus(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Window window = getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                attributes.flags |= flagTranslucentNavigation;
                window.setAttributes(attributes);
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            } else {
                Window window = getWindow();
                WindowManager.LayoutParams attributes = window.getAttributes();
                attributes.flags |= flagTranslucentStatus | flagTranslucentNavigation;
                window.setAttributes(attributes);
            }
        }
    }

    public static int getStatusBarHeight(Context context) {
        Resources resources = context.getResources();
        int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
        int height = resources.getDimensionPixelSize(resourceId);
        return height;
    }

}
